package io.jft.doll.ui.gameHistory

import android.arch.lifecycle.ViewModelProviders
import android.databinding.ObservableField
import android.os.Bundle
import android.support.v7.app.AlertDialog
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.g.base.extend.observeEx
import com.g.base.extend.progressDialog
import com.g.base.extend.setTimeout
import com.g.base.extend.toast
import com.g.base.room.entity.UserEntity
import com.g.base.router.RouteExtras
import com.g.base.ui.BaseActivity
import com.g.base.ui.recyclerView.MultiTypeViewHolder
import io.jft.doll.R
import io.jft.doll.databinding.ActivityGameDetailBinding
import io.jft.doll.model.viewModel.GameHistoryViewModel
import io.jft.doll.room.entity.remote.GameHistoryListREntity
import io.jft.doll.router.RouterPage
import io.jft.doll.ui.gameHistory.bind.GameHistoryBind
import org.greenrobot.eventbus.EventBus

/**
 * Created by G on 2017/12/6 0006.
 */
@Route(path = RouterPage.GameHistoryDetailActivity, extras = RouteExtras.NeedOauth)
class GameDetailActivity : BaseActivity<ActivityGameDetailBinding>() {
    override var hasToolbar: Boolean = true

    private val gameHistoryViewModel by lazy { ViewModelProviders.of(this).get(GameHistoryViewModel::class.java) }
    val feedbackText = ObservableField<String>()
    val feedbackTip = ObservableField<String>()
    val feedbackEnable = ObservableField<Boolean>()


    @JvmField
    @Autowired
    var data: GameHistoryListREntity? = null

    private val reasonList = arrayOf("游戏中系统切维护",
            "娃娃被爪子卡在洞口正上方",
            "娃娃掉进洞口却显示抓取失败",
            "视频卡主不动/卡顿不流畅",
            "游戏中突然闪退/退出应用",
            "爪子没反应,无法移动",
            "视频黑屏",
            "没能成功上机就扣费",
            "其他问题")

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ARouter.getInstance().inject(this)
        setContentView(R.layout.activity_game_detail)
        setStatus(data!!.status)

        toolbar.title = "游戏详情"
        toolbar.setNavigationIcon(R.drawable.ic_arrow_back_toolbar_24dp)
        toolbar.setOnClickListener { finishAfterTransition() }

        contentView.data = this
        MultiTypeViewHolder(contentView.itemBind!!).bindTo(GameHistoryBind(data!!, false))

        showContentView()
    }

    override fun onTokenChange(data: UserEntity?) {
        if (data == null)
            showNeedOauth()
        else
            showContentView()
    }

    fun onFeedbackClick(view: View) {
        AlertDialog.Builder(this)
                .apply {
                    var choice = -1
                    setSingleChoiceItems(
                            reasonList,
                            -1
                    ) { _, i ->
                        choice = i
                    }
                    setNegativeButton("我再想想", null)
                    setPositiveButton("提交申诉") { _, _ ->
                        if (choice == -1) {
                            toast("请选择一个申诉理由")
                        } else {
                            commitFeedback(choice)
                        }
                    }
                    show()
                }
    }

    private fun setStatus(status: Int) {
        feedbackText.set(if (status > 2) "已申诉" else "我要申诉")
        feedbackTip.set(if (status > 2) "您已反馈问题,处理结果将在'我的消息'中通知您" else "如果您在游戏中遇到问题请点击申诉按钮反馈")
        feedbackEnable.set(status <= 2)
    }

    private fun commitFeedback(choice: Int) {
        setTimeout(300L) {
            gameHistoryViewModel.postFeedback(data!!.id, reasonList[choice])
                    .progressDialog(
                            {
                                it.dismiss(true)
                                EventBus.getDefault().post(GameHistoryFeedbackChangeEvent(data!!.id))
                                toast("你的申请已受理 我们将尽快处理")
                                setStatus(data!!.status + 2)
                            },
                            { it.dismiss(true) }
                    )
                    .observeEx(this) {}
        }
    }
}

data class GameHistoryFeedbackChangeEvent(val id: String)